<!--班车站点页-->
<template>

    <div style="height: 100%;">
        <m-page-list
                ref="pageListRef"
                :option="option"
                :page="page"
                :total="total"
                :table-data="tableData"
                :table-loading="tableLoading"
                @search-data="searchData"
        />
        <m-dialog-form ref="dialogRef" :data="dialogData" v-model:show="dialogShow" :option="dialogOption" @confirm="dialogConfirm" ></m-dialog-form>

        <!--批量修改-->
        <m-dialog-form ref="batchRef" :data="batchData" v-model:show="batchShow" :option="batchOption" @confirm="batchConfirm" ></m-dialog-form>

    </div>

</template>

<script setup>

  import {ref,reactive,onMounted,nextTick} from 'vue'
  import {useRouter} from 'vue-router'
  //组件
  import MPageList from '@/components/PageList'
  import MDialogForm from '@/components/DialogForm/DialogForm'
  //请求
  import {getShuttleStop, addShuttleStop, editShuttleStop,editShuttleStopList, delShuttleStop,delShuttleStopList,getSchoolStop,getShuttle,getShuttleDirection,getStop} from '@/request/api/basicData'

  import {usePageList} from "../../composables/pageList";
  //引用类型字段参数
  import {refOpt} from "../const";

  const router = useRouter();

  let opts = {
    auth_id: 'shuttleStop',
    getApi: getShuttleStop,addApi:addShuttleStop,editApi:editShuttleStop,delApi:delShuttleStop,
    idKey: 'packag_type_id'
  };
  let {option,page,pageLoading,total,tableLoading,tableData,query,dialogRef,dialogShow,dialogData,dialogOption,
    searchData,getDataList,dialogConfirm} = usePageList(opts);
  //开启表格选择
  option.tableSelection = true;
  option.conditions = [
    {label: '班车编号',type: 'text',field_id: 'shuttle_id' },
    {label: '站点编号',type: 'text',field_id: 'stop_id'},
    {label: '班车方向编号',type: 'text',field_id: 'shuttle_direction_id'},
  ];
  option.cols = [
    {prop: 'shuttle_name',label: '班车名称' },
    {prop: 'shuttle_direction_name',label: '班车方向名称' },
    {prop: 'stop_name',label: '班车方向名称' },
    {prop: 'sort',label: '序号', },
    {prop: 'sale_minimum',label: '可售最少数', },
    {prop: 'departure_time',label: '发车时间', },
    {prop: 'arrive_time',label: '到达时间'},
    {prop: 'mark',label: '说明', },
    {prop: 'is_used',label: '使用标识',type: 'bool' },
    {prop: 'input_id',label: '录入员号' },
    {prop: 'input_name',label: '录入员名' },
    {prop: 'input_time',label: '录入时间' },
    {prop: 'modify_id',label: '修改员号' },
    {prop: 'modify_name',label: '修改员名' },
    {prop: 'modify_time',label: '修改时间' },
  ];
  option.hLBtns.push(
    {title: '批量修改',id: 'batchUpdate',type: 'primary',event: batchUpdate},
    {title: '批量删除',id: 'batchDel',type: 'primary',event: batchDel}
  )
  dialogOption.fieldList = [

    {...refOpt.shuttle_id,col:2},
    {...refOpt.shuttle_name,editDisabled: true,col:2},
    {...refOpt.stop_id,col:2},
    {...refOpt.stop_name,editDisabled: true,col:2},
    {...refOpt.school_id,col:2},
    {...refOpt.school_name,editDisabled: true,col:2},
    {...refOpt.shuttle_direction_id,col:2},
    {...refOpt.shuttle_direction_name,editDisabled: true,col:2},

    /*{field_id: 'shuttle_id',label: '班车编号',type: 'text',col:2,disabled: true },
    {field_id: 'shuttle_name',label: '班车名称',type: 'reference',col:2 ,editDisabled: true,refUrl: getShuttle,
      refCols: [
        {prop: 'shuttle_id',label: '班车编号' },
        {prop: 'shuttle_name',label: '班车名称' },],entityKey: 'shuttle_name', valueKey: ['shuttle_id','shuttle_name']  },
    {field_id: 'stop_id',label: '站点编号',type: 'text',col:2,disabled: true },
    {field_id: 'stop_name',label: '站点名称',type: 'reference',col:2 ,editDisabled: true,refUrl: getStop,
      refCols: [
        {prop: 'stop_id', label: '站点编号',},
        {prop: 'stop_name', label: '站点名称',},
      ],entityKey: 'stop_name',valueKey: ['stop_id','stop_name']},
    {field_id: 'shuttle_direction_id',label: '班车方向编号',type: 'text',col:2 ,disabled: true },
    {field_id: 'shuttle_direction_name',label: '班车方向名称',type: 'reference',col:2,editDisabled: true, refUrl: getShuttleDirection,
      refCols: [
        {prop: 'shuttle_direction_id', label: '班车方向编号',},
        {prop: 'shuttle_direction_name', label: '班车方向名称',},
      ],entityKey: 'shuttle_direction_name',valueKey: ['shuttle_direction_id','shuttle_direction_name']},*/
    {field_id: 'sort',label: '序号',type: 'number',col:2 },
    {field_id: 'sale_minimum',label: '可售最少数',type: 'number',col:2 },
    {field_id: 'departure_time',label: '发车时间',type: 'datetime' ,col:2},
    {field_id: 'arrive_time',label: '到达时间',type: 'datetime',col:2 },
    {field_id: 'mark',label: '说明',type: 'textarea',col:4 },
    {field_id: 'is_used',label: '使用标识',type: 'bool' ,col:2},
  ];

  let pageListRef = ref(null),
    batchRef = ref(null),
    batchOption = {
      title: '批量修改',
      fieldList: [
        {field_id: 'sale_minimum',label: '可售最少数',type: 'number',col:2 ,precision: 1},
        {col:2 },
        {field_id: 'departure_time',label: '发车时间',type: 'datetime' ,col:2},
        {field_id: 'arrive_time',label: '到达时间',type: 'datetime',col:2 },
        {field_id: 'mark',label: '说明',type: 'textarea',col:4 },
        {field_id: 'is_used',label: '使用标识',type: 'bool' ,col:2},
      ]
    },
    batchData = ref({}),
    batchShow = ref(false),
    selectedRows = [];
  //批量修改
  function batchUpdate() {
    let rows = pageListRef.value.getSelectionRows();
    if (!rows.length){
      ElMessage({type: 'warning',message: '请先选择数据行'});
      return
    }
    selectedRows = rows;
    batchShow.value = true;
  }
  function batchConfirm(obj) {
    pageLoading.value = true;
    let {data} = obj,
      list = [];
    selectedRows.forEach(item => {
      let {shuttle_id,stop_id,shuttle_direction_id} = item;
      list.push({
        ...data,
        shuttle_id,stop_id,shuttle_direction_id
      })
    });
    editShuttleStopList(list).then(res =>{
      pageLoading.value = false;
      if (res.errcode !== 0)return;
      getDataList();
      batchRef.value.close();

    })
  }

  //批量删除
  function batchDel() {
    let rows = pageListRef.value.getSelectionRows();
    if (!rows.length) {
      ElMessage({type: 'warning', message: '请先选择数据行'});
      return;
    }
    ElMessageBox.confirm('确定删除所选数据？', '批量删除', {
      'cancelButtonText': '取消',
      'confirmButtonText': '确定',
      callback: (action) => {
        if (action === 'confirm'){
          pageLoading.value = true;
          let list = [];
          rows.forEach(item => {
            let {shuttle_id,stop_id,shuttle_direction_id} = item;
            list.push({shuttle_id,stop_id,shuttle_direction_id})
          });

          delShuttleStopList(list).then(res => {
            pageLoading.value = false;
            if (res.errcode !== 0)return;
            ElMessage({
              type: 'success',
              message: `删除成功`,
            });
            getDataList();
          })
        }
      },
    })

  }

</script>

<style scoped lang="less">
    @import '../../assets/css/pageList.less';

</style>
